package registnet.dao;


import java.math.BigDecimal;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.apache.torque.TorqueException;
import org.apache.torque.om.BaseObject;
import org.apache.torque.om.ComboKey;
import org.apache.torque.om.DateKey;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.ObjectKey;
import org.apache.torque.om.SimpleKey;
import org.apache.torque.om.StringKey;
import org.apache.torque.om.Persistent;
import org.apache.torque.util.Criteria;
import org.apache.torque.util.Transaction;

  
  
/**
 * This class was autogenerated by Torque on:
 *
 * [Tue Jun 30 14:38:16 PDT 2009]
 *
 * You should not use this class directly.  It should not even be
 * extended all references should be to Unit
 */
public abstract class BaseUnit extends BaseObject
{
    /** The Peer class */
    private static final UnitPeer peer =
        new UnitPeer();

        
    /** The value for the unitId field */
    private int unitId;
      
    /** The value for the unitCode field */
    private String unitCode;
      
    /** The value for the unitName field */
    private String unitName;
      
    /** The value for the unitType field */
    private String unitType;
      
    /** The value for the collegeId field */
    private int collegeId;
  
    
    /**
     * Get the UnitId
     *
     * @return int
     */
    public int getUnitId()
    {
        return unitId;
    }

                                              
    /**
     * Set the value of UnitId
     *
     * @param v new value
     */
    public void setUnitId(int v) throws TorqueException
    {
    
                  if (this.unitId != v)
              {
            this.unitId = v;
            setModified(true);
        }
    
          
                                  
        // update associated Course
        if (collCourses != null)
        {
            for (int i = 0; i < collCourses.size(); i++)
            {
                ((Course) collCourses.get(i))
                    .setUnitId(v);
            }
        }
                                          
        // update associated Employee
        if (collEmployees != null)
        {
            for (int i = 0; i < collEmployees.size(); i++)
            {
                ((Employee) collEmployees.get(i))
                    .setUnitId(v);
            }
        }
                                          
        // update associated UnitUser
        if (collUnitUsers != null)
        {
            for (int i = 0; i < collUnitUsers.size(); i++)
            {
                ((UnitUser) collUnitUsers.get(i))
                    .setUnitId(v);
            }
        }
                      }
  
    /**
     * Get the UnitCode
     *
     * @return String
     */
    public String getUnitCode()
    {
        return unitCode;
    }

                        
    /**
     * Set the value of UnitCode
     *
     * @param v new value
     */
    public void setUnitCode(String v) 
    {
    
                  if (!ObjectUtils.equals(this.unitCode, v))
              {
            this.unitCode = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the UnitName
     *
     * @return String
     */
    public String getUnitName()
    {
        return unitName;
    }

                        
    /**
     * Set the value of UnitName
     *
     * @param v new value
     */
    public void setUnitName(String v) 
    {
    
                  if (!ObjectUtils.equals(this.unitName, v))
              {
            this.unitName = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the UnitType
     *
     * @return String
     */
    public String getUnitType()
    {
        return unitType;
    }

                        
    /**
     * Set the value of UnitType
     *
     * @param v new value
     */
    public void setUnitType(String v) 
    {
    
                  if (!ObjectUtils.equals(this.unitType, v))
              {
            this.unitType = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the CollegeId
     *
     * @return int
     */
    public int getCollegeId()
    {
        return collegeId;
    }

                              
    /**
     * Set the value of CollegeId
     *
     * @param v new value
     */
    public void setCollegeId(int v) throws TorqueException
    {
    
                  if (this.collegeId != v)
              {
            this.collegeId = v;
            setModified(true);
        }
    
                          
                if (aCollege != null && !(aCollege.getCollegeId() == v))
                {
            aCollege = null;
        }
      
              }
  
      
    
                  
    
        private College aCollege;

    /**
     * Declares an association between this object and a College object
     *
     * @param v College
     * @throws TorqueException
     */
    public void setCollege(College v) throws TorqueException
    {
            if (v == null)
        {
                    setCollegeId(0);
                  }
        else
        {
            setCollegeId(v.getCollegeId());
        }
                aCollege = v;
    }

                                            
    /**
     * Get the associated College object
     *
     * @return the associated College object
     * @throws TorqueException
     */
    public College getCollege() throws TorqueException
    {
        if (aCollege == null && (this.collegeId > 0))
        {
                          aCollege = CollegePeer.retrieveByPK(SimpleKey.keyFor(this.collegeId));
              
            /* The following can be used instead of the line above to
               guarantee the related object contains a reference
               to this object, but this level of coupling
               may be undesirable in many circumstances.
               As it can lead to a db query with many results that may
               never be used.
               College obj = CollegePeer.retrieveByPK(this.collegeId);
               obj.addUnits(this);
            */
        }
        return aCollege;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey.  e.g.
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
           */
    public void setCollegeKey(ObjectKey key) throws TorqueException
    {
      
                        setCollegeId(((NumberKey) key).intValue());
                  }
       
                                
            
    /**
     * Collection to store aggregation of collCourses
     */
    protected List collCourses;

    /**
     * Temporary storage of collCourses to save a possible db hit in
     * the event objects are add to the collection, but the
     * complete collection is never requested.
     */
    protected void initCourses()
    {
        if (collCourses == null)
        {
            collCourses = new ArrayList();
        }
    }

    /**
     * Method called to associate a Course object to this object
     * through the Course foreign key attribute
     *
     * @param l Course
     * @throws TorqueException
     */
    public void addCourse(Course l) throws TorqueException
    {
        getCourses().add(l);
        l.setUnit((Unit) this);
    }

    /**
     * The criteria used to select the current contents of collCourses
     */
    private Criteria lastCoursesCriteria = null;

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * getCourses(new Criteria())
     *
     * @throws TorqueException
     */
    public List getCourses() throws TorqueException
    {
        if (collCourses == null)
        {
            collCourses = getCourses(new Criteria(10));
        }
        return collCourses;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Unit has previously
     * been saved, it will retrieve related Courses from storage.
     * If this Unit is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     *
     * @throws TorqueException
     */
    public List getCourses(Criteria criteria) throws TorqueException
    {
        if (collCourses == null)
        {
            if (isNew())
            {
               collCourses = new ArrayList();
            }
            else
            {
                      criteria.add(CoursePeer.UNITID, getUnitId() );
                      collCourses = CoursePeer.doSelect(criteria);
            }
        }
        else
        {
            // criteria has no effect for a new object
            if (!isNew())
            {
                // the following code is to determine if a new query is
                // called for.  If the criteria is the same as the last
                // one, just return the collection.
                      criteria.add(CoursePeer.UNITID, getUnitId());
                      if (!lastCoursesCriteria.equals(criteria))
                {
                    collCourses = CoursePeer.doSelect(criteria);
                }
            }
        }
        lastCoursesCriteria = criteria;

        return collCourses;
    }

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * getCourses(new Criteria(),Connection)
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List getCourses(Connection con) throws TorqueException
    {
        if (collCourses == null)
        {
            collCourses = getCourses(new Criteria(10), con);
        }
        return collCourses;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Unit has previously
     * been saved, it will retrieve related Courses from storage.
     * If this Unit is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List getCourses(Criteria criteria, Connection con)
            throws TorqueException
    {
        if (collCourses == null)
        {
            if (isNew())
            {
               collCourses = new ArrayList();
            }
            else
            {
                       criteria.add(CoursePeer.UNITID, getUnitId());
                       collCourses = CoursePeer.doSelect(criteria, con);
             }
         }
         else
         {
             // criteria has no effect for a new object
             if (!isNew())
             {
                 // the following code is to determine if a new query is
                 // called for.  If the criteria is the same as the last
                 // one, just return the collection.
                       criteria.add(CoursePeer.UNITID, getUnitId());
                       if (!lastCoursesCriteria.equals(criteria))
                 {
                     collCourses = CoursePeer.doSelect(criteria, con);
                 }
             }
         }
         lastCoursesCriteria = criteria;

         return collCourses;
     }

                  
              
                    
                              
                                
                                                              
                                        
                    
                    
          
    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Unit is new, it will return
     * an empty collection; or if this Unit has previously
     * been saved, it will retrieve related Courses from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Unit.
     */
    protected List getCoursesJoinUnit(Criteria criteria)
        throws TorqueException
    {
        if (collCourses == null)
        {
            if (isNew())
            {
               collCourses = new ArrayList();
            }
            else
            {
                            criteria.add(CoursePeer.UNITID, getUnitId());
                            collCourses = CoursePeer.doSelectJoinUnit(criteria);
            }
        }
        else
        {
            // the following code is to determine if a new query is
            // called for.  If the criteria is the same as the last
            // one, just return the collection.
            boolean newCriteria = true;
                        criteria.add(CoursePeer.UNITID, getUnitId());
                        if (!lastCoursesCriteria.equals(criteria))
            {
                collCourses = CoursePeer.doSelectJoinUnit(criteria);
            }
        }
        lastCoursesCriteria = criteria;

        return collCourses;
    }
                            


                          
            
    /**
     * Collection to store aggregation of collEmployees
     */
    protected List collEmployees;

    /**
     * Temporary storage of collEmployees to save a possible db hit in
     * the event objects are add to the collection, but the
     * complete collection is never requested.
     */
    protected void initEmployees()
    {
        if (collEmployees == null)
        {
            collEmployees = new ArrayList();
        }
    }

    /**
     * Method called to associate a Employee object to this object
     * through the Employee foreign key attribute
     *
     * @param l Employee
     * @throws TorqueException
     */
    public void addEmployee(Employee l) throws TorqueException
    {
        getEmployees().add(l);
        l.setUnit((Unit) this);
    }

    /**
     * The criteria used to select the current contents of collEmployees
     */
    private Criteria lastEmployeesCriteria = null;

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * getEmployees(new Criteria())
     *
     * @throws TorqueException
     */
    public List getEmployees() throws TorqueException
    {
        if (collEmployees == null)
        {
            collEmployees = getEmployees(new Criteria(10));
        }
        return collEmployees;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Unit has previously
     * been saved, it will retrieve related Employees from storage.
     * If this Unit is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     *
     * @throws TorqueException
     */
    public List getEmployees(Criteria criteria) throws TorqueException
    {
        if (collEmployees == null)
        {
            if (isNew())
            {
               collEmployees = new ArrayList();
            }
            else
            {
                      criteria.add(EmployeePeer.UNITID, getUnitId() );
                      collEmployees = EmployeePeer.doSelect(criteria);
            }
        }
        else
        {
            // criteria has no effect for a new object
            if (!isNew())
            {
                // the following code is to determine if a new query is
                // called for.  If the criteria is the same as the last
                // one, just return the collection.
                      criteria.add(EmployeePeer.UNITID, getUnitId());
                      if (!lastEmployeesCriteria.equals(criteria))
                {
                    collEmployees = EmployeePeer.doSelect(criteria);
                }
            }
        }
        lastEmployeesCriteria = criteria;

        return collEmployees;
    }

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * getEmployees(new Criteria(),Connection)
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List getEmployees(Connection con) throws TorqueException
    {
        if (collEmployees == null)
        {
            collEmployees = getEmployees(new Criteria(10), con);
        }
        return collEmployees;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Unit has previously
     * been saved, it will retrieve related Employees from storage.
     * If this Unit is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List getEmployees(Criteria criteria, Connection con)
            throws TorqueException
    {
        if (collEmployees == null)
        {
            if (isNew())
            {
               collEmployees = new ArrayList();
            }
            else
            {
                       criteria.add(EmployeePeer.UNITID, getUnitId());
                       collEmployees = EmployeePeer.doSelect(criteria, con);
             }
         }
         else
         {
             // criteria has no effect for a new object
             if (!isNew())
             {
                 // the following code is to determine if a new query is
                 // called for.  If the criteria is the same as the last
                 // one, just return the collection.
                       criteria.add(EmployeePeer.UNITID, getUnitId());
                       if (!lastEmployeesCriteria.equals(criteria))
                 {
                     collEmployees = EmployeePeer.doSelect(criteria, con);
                 }
             }
         }
         lastEmployeesCriteria = criteria;

         return collEmployees;
     }

                  
              
                    
                              
                                
                                                              
                                        
                    
                    
          
    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Unit is new, it will return
     * an empty collection; or if this Unit has previously
     * been saved, it will retrieve related Employees from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Unit.
     */
    protected List getEmployeesJoinUnit(Criteria criteria)
        throws TorqueException
    {
        if (collEmployees == null)
        {
            if (isNew())
            {
               collEmployees = new ArrayList();
            }
            else
            {
                            criteria.add(EmployeePeer.UNITID, getUnitId());
                            collEmployees = EmployeePeer.doSelectJoinUnit(criteria);
            }
        }
        else
        {
            // the following code is to determine if a new query is
            // called for.  If the criteria is the same as the last
            // one, just return the collection.
            boolean newCriteria = true;
                        criteria.add(EmployeePeer.UNITID, getUnitId());
                        if (!lastEmployeesCriteria.equals(criteria))
            {
                collEmployees = EmployeePeer.doSelectJoinUnit(criteria);
            }
        }
        lastEmployeesCriteria = criteria;

        return collEmployees;
    }
                            


                          
            
    /**
     * Collection to store aggregation of collUnitUsers
     */
    protected List collUnitUsers;

    /**
     * Temporary storage of collUnitUsers to save a possible db hit in
     * the event objects are add to the collection, but the
     * complete collection is never requested.
     */
    protected void initUnitUsers()
    {
        if (collUnitUsers == null)
        {
            collUnitUsers = new ArrayList();
        }
    }

    /**
     * Method called to associate a UnitUser object to this object
     * through the UnitUser foreign key attribute
     *
     * @param l UnitUser
     * @throws TorqueException
     */
    public void addUnitUser(UnitUser l) throws TorqueException
    {
        getUnitUsers().add(l);
        l.setUnit((Unit) this);
    }

    /**
     * The criteria used to select the current contents of collUnitUsers
     */
    private Criteria lastUnitUsersCriteria = null;

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * getUnitUsers(new Criteria())
     *
     * @throws TorqueException
     */
    public List getUnitUsers() throws TorqueException
    {
        if (collUnitUsers == null)
        {
            collUnitUsers = getUnitUsers(new Criteria(10));
        }
        return collUnitUsers;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Unit has previously
     * been saved, it will retrieve related UnitUsers from storage.
     * If this Unit is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     *
     * @throws TorqueException
     */
    public List getUnitUsers(Criteria criteria) throws TorqueException
    {
        if (collUnitUsers == null)
        {
            if (isNew())
            {
               collUnitUsers = new ArrayList();
            }
            else
            {
                      criteria.add(UnitUserPeer.UNITID, getUnitId() );
                      collUnitUsers = UnitUserPeer.doSelect(criteria);
            }
        }
        else
        {
            // criteria has no effect for a new object
            if (!isNew())
            {
                // the following code is to determine if a new query is
                // called for.  If the criteria is the same as the last
                // one, just return the collection.
                      criteria.add(UnitUserPeer.UNITID, getUnitId());
                      if (!lastUnitUsersCriteria.equals(criteria))
                {
                    collUnitUsers = UnitUserPeer.doSelect(criteria);
                }
            }
        }
        lastUnitUsersCriteria = criteria;

        return collUnitUsers;
    }

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * getUnitUsers(new Criteria(),Connection)
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List getUnitUsers(Connection con) throws TorqueException
    {
        if (collUnitUsers == null)
        {
            collUnitUsers = getUnitUsers(new Criteria(10), con);
        }
        return collUnitUsers;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Unit has previously
     * been saved, it will retrieve related UnitUsers from storage.
     * If this Unit is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List getUnitUsers(Criteria criteria, Connection con)
            throws TorqueException
    {
        if (collUnitUsers == null)
        {
            if (isNew())
            {
               collUnitUsers = new ArrayList();
            }
            else
            {
                       criteria.add(UnitUserPeer.UNITID, getUnitId());
                       collUnitUsers = UnitUserPeer.doSelect(criteria, con);
             }
         }
         else
         {
             // criteria has no effect for a new object
             if (!isNew())
             {
                 // the following code is to determine if a new query is
                 // called for.  If the criteria is the same as the last
                 // one, just return the collection.
                       criteria.add(UnitUserPeer.UNITID, getUnitId());
                       if (!lastUnitUsersCriteria.equals(criteria))
                 {
                     collUnitUsers = UnitUserPeer.doSelect(criteria, con);
                 }
             }
         }
         lastUnitUsersCriteria = criteria;

         return collUnitUsers;
     }

                  
              
                    
                              
                                
                                                              
                                        
                    
                    
          
    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Unit is new, it will return
     * an empty collection; or if this Unit has previously
     * been saved, it will retrieve related UnitUsers from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Unit.
     */
    protected List getUnitUsersJoinUnit(Criteria criteria)
        throws TorqueException
    {
        if (collUnitUsers == null)
        {
            if (isNew())
            {
               collUnitUsers = new ArrayList();
            }
            else
            {
                            criteria.add(UnitUserPeer.UNITID, getUnitId());
                            collUnitUsers = UnitUserPeer.doSelectJoinUnit(criteria);
            }
        }
        else
        {
            // the following code is to determine if a new query is
            // called for.  If the criteria is the same as the last
            // one, just return the collection.
            boolean newCriteria = true;
                        criteria.add(UnitUserPeer.UNITID, getUnitId());
                        if (!lastUnitUsersCriteria.equals(criteria))
            {
                collUnitUsers = UnitUserPeer.doSelectJoinUnit(criteria);
            }
        }
        lastUnitUsersCriteria = criteria;

        return collUnitUsers;
    }
                            


          
    private static List fieldNames = null;

    /**
     * Generate a list of field names.
     *
     * @return a list of field names
     */
    public static synchronized List getFieldNames()
    {
        if (fieldNames == null)
        {
            fieldNames = new ArrayList();
              fieldNames.add("UnitId");
              fieldNames.add("UnitCode");
              fieldNames.add("UnitName");
              fieldNames.add("UnitType");
              fieldNames.add("CollegeId");
              fieldNames = Collections.unmodifiableList(fieldNames);
        }
        return fieldNames;
    }

    /**
     * Retrieves a field from the object by name passed in as a String.
     *
     * @param name field name
     * @return value
     */
    public Object getByName(String name)
    {
          if (name.equals("UnitId"))
        {
                return new Integer(getUnitId());
            }
          if (name.equals("UnitCode"))
        {
                return getUnitCode();
            }
          if (name.equals("UnitName"))
        {
                return getUnitName();
            }
          if (name.equals("UnitType"))
        {
                return getUnitType();
            }
          if (name.equals("CollegeId"))
        {
                return new Integer(getCollegeId());
            }
          return null;
    }
    
    /**
     * Retrieves a field from the object by name passed in
     * as a String.  The String must be one of the static
     * Strings defined in this Class' Peer.
     *
     * @param name peer name
     * @return value
     */
    public Object getByPeerName(String name)
    {
          if (name.equals(UnitPeer.UNITID))
        {
                return new Integer(getUnitId());
            }
          if (name.equals(UnitPeer.UNITCODE))
        {
                return getUnitCode();
            }
          if (name.equals(UnitPeer.UNITNAME))
        {
                return getUnitName();
            }
          if (name.equals(UnitPeer.UNITTYPE))
        {
                return getUnitType();
            }
          if (name.equals(UnitPeer.COLLEGEID))
        {
                return new Integer(getCollegeId());
            }
          return null;
    }

    /**
     * Retrieves a field from the object by Position as specified
     * in the xml schema.  Zero-based.
     *
     * @param pos position in xml schema
     * @return value
     */
    public Object getByPosition(int pos)
    {
            if (pos == 0)
        {
                return new Integer(getUnitId());
            }
              if (pos == 1)
        {
                return getUnitCode();
            }
              if (pos == 2)
        {
                return getUnitName();
            }
              if (pos == 3)
        {
                return getUnitType();
            }
              if (pos == 4)
        {
                return new Integer(getCollegeId());
            }
              return null;
    }
     
    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     *
     * @throws Exception
     */
    public void save() throws Exception
    {
          save(UnitPeer.getMapBuilder()
                .getDatabaseMap().getName());
      }

    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
       * Note: this code is here because the method body is
     * auto-generated conditionally and therefore needs to be
     * in this file instead of in the super class, BaseObject.
       *
     * @param dbName
     * @throws TorqueException
     */
    public void save(String dbName) throws TorqueException
    {
        Connection con = null;
          try
        {
            con = Transaction.begin(dbName);
            save(con);
            Transaction.commit(con);
        }
        catch(TorqueException e)
        {
            Transaction.safeRollback(con);
            throw e;
        }
      }

      /** flag to prevent endless save loop, if this object is referenced
        by another object which falls in this transaction. */
    private boolean alreadyInSave = false;
      /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.  This method
     * is meant to be used as part of a transaction, otherwise use
     * the save() method and the connection details will be handled
     * internally
     *
     * @param con
     * @throws TorqueException
     */
    public void save(Connection con) throws TorqueException
    {
          if (!alreadyInSave)
        {
            alreadyInSave = true;


  
            // If this object has been modified, then save it to the database.
            if (isModified())
            {
                if (isNew())
                {
                    UnitPeer.doInsert((Unit) this, con);
                    setNew(false);
                }
                else
                {
                    UnitPeer.doUpdate((Unit) this, con);
                }
            }

                                      
                
            if (collCourses != null)
            {
                for (int i = 0; i < collCourses.size(); i++)
                {
                    ((Course) collCourses.get(i)).save(con);
                }
            }
                                          
                
            if (collEmployees != null)
            {
                for (int i = 0; i < collEmployees.size(); i++)
                {
                    ((Employee) collEmployees.get(i)).save(con);
                }
            }
                                          
                
            if (collUnitUsers != null)
            {
                for (int i = 0; i < collUnitUsers.size(); i++)
                {
                    ((UnitUser) collUnitUsers.get(i)).save(con);
                }
            }
                          alreadyInSave = false;
        }
      }


                          
      /**
     * Set the PrimaryKey using ObjectKey.
     *
     * @param  unitId ObjectKey
     */
    public void setPrimaryKey(ObjectKey key)
        throws TorqueException
    {
            setUnitId(((NumberKey) key).intValue());
        }

    /**
     * Set the PrimaryKey using a String.
     *
     * @param key
     */
    public void setPrimaryKey(String key) throws TorqueException
    {
            setUnitId(Integer.parseInt(key));
        }

  
    /**
     * returns an id that differentiates this object from others
     * of its class.
     */
    public ObjectKey getPrimaryKey()
    {
          return SimpleKey.keyFor(getUnitId());
      }

 

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
       * It then fills all the association collections and sets the
     * related objects to isNew=true.
       */
      public Unit copy() throws TorqueException
    {
        return copyInto(new Unit());
    }
  
    protected Unit copyInto(Unit copyObj) throws TorqueException
    {
          copyObj.setUnitId(unitId);
          copyObj.setUnitCode(unitCode);
          copyObj.setUnitName(unitName);
          copyObj.setUnitType(unitType);
          copyObj.setCollegeId(collegeId);
  
                    copyObj.setUnitId(0);
                                    
                                      
                
        List v = getCourses();
        for (int i = 0; i < v.size(); i++)
        {
            Course obj = (Course) v.get(i);
            copyObj.addCourse(obj.copy());
        }
                                                  
                
        v = getEmployees();
        for (int i = 0; i < v.size(); i++)
        {
            Employee obj = (Employee) v.get(i);
            copyObj.addEmployee(obj.copy());
        }
                                                  
                
        v = getUnitUsers();
        for (int i = 0; i < v.size(); i++)
        {
            UnitUser obj = (UnitUser) v.get(i);
            copyObj.addUnitUser(obj.copy());
        }
                    
        return copyObj;
    }

    /**
     * returns a peer instance associated with this om.  Since Peer classes
     * are not to have any instance attributes, this method returns the
     * same instance for all member of this class. The method could therefore
     * be static, but this would prevent one from overriding the behavior.
     */
    public UnitPeer getPeer()
    {
        return peer;
    }

    public String toString()
    {
        StringBuffer str = new StringBuffer();
        str.append("Unit:\n");
        str.append("UnitId = ")
           .append(getUnitId())
           .append("\n");
        str.append("UnitCode = ")
           .append(getUnitCode())
           .append("\n");
        str.append("UnitName = ")
           .append(getUnitName())
           .append("\n");
        str.append("UnitType = ")
           .append(getUnitType())
           .append("\n");
        str.append("CollegeId = ")
           .append(getCollegeId())
           .append("\n");
        return(str.toString());
    }
}
